package erfgame.core.util;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import erfgame.core.util.RangeUtils;

public class SubtractRangeTester {
	
	@Test
	public void testLeftChop() {
		short[] f = {1,3};
		short[] s = {2,4};
		short[] er = {1,2};
		short[] ar = new short[5];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testRightChop() {
		short[] f = {3,6};
		short[] s = {2,4};
		short[] er = {4,6};
		short[] ar = new short[5];
		int count = RangeUtils.subtractRange( f, s, s.length,ar );
		assertEqual( er, ar, count );
	}
	
	@Test
	public void testIntersect() {
		short[] f = {0,8};
		short[] s = {2,4};
		short[] er = {0,2,4,8};
		short[] ar = new short[9];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}
	
	@Test
	public void testDelete() {
		short[] f = {3,4};
		short[] s = {2,5};
		short[] er = {};
		short[] ar = new short[5];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testDeleteLong() {
		short[] f = {3,4,6,7,8,9};
		short[] s = {2,5};
		short[] er = {6,7,8,9};
		short[] ar = new short[9];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testSame() {
		short[] f = {3,4};
		short[] s = {3,4};
		short[] er = {};
		short[] ar = new short[5];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}
	
	@Test
	public void testChopLong() {
		short[] f = {0,8,10,12};
		short[] s = {6,9};
		short[] er = {0,6,10,12};
		short[] ar = new short[9];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testChopLongAdjust() {
		short[] f = {0,8,10,12};
		short[] s = {6,9};
		short[] er = {0,6,10,12};
		short[] ar = new short[9];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testInterleaved() {
		short[] f = {0,5,10,12};
		short[] s = {5,10};
		short[] er = {0,5,10,12};
		short[] ar = new short[9];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testMultiChop() {
		short[] f = {0,6,9,16};
		short[] s = {5,10,15,20};
		short[] er = {0,5,10,15};
		short[] ar = new short[9];
		int count = RangeUtils.subtractRange( f, s, s.length, ar );
		assertEqual( er, ar, count );
	}

	@Test
	public void testRealData() {
//		short[] f = {95,106,150,200};
//		short[] s = {95,107,151,200};
//		short[] er = {95,96,150,152};
//		short[] ar = new short[9];
//		int count = TerrainBlockRenderer.subtractRange( f, s, s.length, ar );
//		assertEqual( er, ar, count );
	}


	private void assertEqual( short[] in, short[] out, int length ) {
		assertEquals( in.length, length );
		for( int i=0; i<length; i++ ) {
			assertEquals( in[i], out[i] );
		}
	}
}
